<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width='isCollapse ? "64px" : "200px"'>
        <div class="toggle-button" @click='toggleMenuCollapse'>|||</div>
        <el-menu :default-active='activeKey' :collapse-transition='false' :collapse='isCollapse' router
          :unique-opened='true' background-color="#333744" text-color='#fff' active-text-color='#ffd04b'>
          <el-submenu :index='"/" + submenu1.id' v-for='submenu1 in asideList' :key='submenu1.id'>
            <template slot="title">
              <i :class="iconObj[submenu1.id]"></i>
              <span>{{ submenu1.authName }}</span>
            </template>
            <el-menu-item @click='saveActiveKey("/" + submenu2.path)' :index='"/" + submenu2.path'
              v-for="submenu2 in submenu1.children" :key="submenu2.id">
              <i class="el-icon-menu"></i>
              <span slot="title">{{ submenu2.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
  export default {
    data() {
      return {
        // aside导航数据
        asideList: null,
        // icon图标对象，渲染一级菜单
        iconObj: {
          '125': 'iconfont icon-users',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        },
        // 菜单是否折叠
        isCollapse: false,
        // 保存菜单激活状态的key
        activeKey: ''
      }
    },
    created() {
      this.getAsideList()
      this.activeKey = window.sessionStorage.getItem('activeKey')
    },
    methods: {
      // 保存菜单激活高亮显示的index
      saveActiveKey(key) {
        console.log(key)
        window.sessionStorage.setItem('activeKey', key)
        this.activeKey = key
      },
      // 切换菜单折叠效果
      toggleMenuCollapse() {
        this.isCollapse = !this.isCollapse
      },
      // 退出登录
      logout() {
        // 清除token
        window.sessionStorage.clear()
        // 编程式导航跳转到login组件
        this.$router.push('/login')
      },
      // 获取aside导航数据
      async getAsideList() {
        const {
          data: res
        } = await this.$http.get('menus')
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error('获取菜单失败')
        this.asideList = res.data
      }
    }
  }

</script>
<style lang="less" scoped>
  .el-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    padding: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;

    >div {
      color: #fff;
      font-size: 18px;

      display: flex;
      align-items: center;

      span {
        margin-left: 20px;
      }
    }

    .el-button {
      margin-right: 20px;
    }
  }

  .el-aside {
    background-color: #333744;

    .toggle-button {
      text-align: center;
      width: 100%;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .iconfont {
    margin-right: 20px;
  }

  .el-menu {
    border-right: none;
  }

  .toggle-button {
    line-height: 30px;
    font-size: 12px;
    color: #fff;
    letter-spacing: .4em;
  }

</style>
